#include "xdeepcontent.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

XSIPDeepContent::XSIPDeepContent()
{
	m_data = NULL;
	m_size = 0;
	m_copyType = DEEP_COPY;
}
	
XSIPDeepContent::~XSIPDeepContent()
{

}

void XSIPDeepContent::copy(void* data, int size)
{
	if (m_data)
		free (m_data);
	m_data = (char*) malloc(size);
	if (data)
		memcpy(m_data, data, size);
	m_size = size;
}

XSIPContent** XSIPDeepContent::divide(int step, int *size)
{
	if (m_size <= step)
	{
		*size = 1;
		XSIPContent* that = this;
		return &that;
	}
	*size = (m_size / step) +( (m_size % step) ? 1 : 0);
	int i = 0;
	XSIPContent** content = new XSIPContent*[*size];
	for (i = 0; i < *size - 1; i ++)
	{
		content[i] = new XSIPContent();
		content[i]->copy((char*)m_data + step * i, step);
	}
	content[i] = new XSIPContent();
	content[i]->copy((char*)m_data + step * i, m_size - (step * i));
	return content;
}

void* XSIPDeepContent::getObject(const char* name) const
{
	if (strcmp(name, "XSIPDeepContent") == 0)
		return const_cast<XSIPDeepContent*>(this);
	else
		return XSIPContent::getObject(name);
}